package com.fks.fks_service.utils.aliutils;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeCreateModel;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradeCreateRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.alipay.api.response.AlipayTradeCreateResponse;

import java.util.Map;

public class AlipayTradeCreate {

    public static String privateKey  = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIB" +
            "AQCQEzgoC7Rdpq7FrsiE7UXjz0yn32VPbcPpSXtg+wiqyo7op0ex9T+7h/jam49vz" +
            "pmp/Ka0zE27P5YqsbJHO90G9jeLw0548Swo4e3sJZQ2403t+OOMbJnNCzCJvmAQ1aeCc" +
            "QsVSfvucsUU3N37BrGeCwqfTDlEijAP9SQHfzJ8TCY5FWT2PTEg7A79fcJh91TFDf3nV" +
            "SA6gxxi+7DRKxY3YydtyTnye6rr6/vDQWUk6pW1s4G6ftrJKs4KfJnKE1WD9zd8xvD5c" +
            "WNObkigVF/sV9JFOaGXgDDrlNj8vCoE9cehHq5kQZT4tl9eRoCmHJ0H0Bor7nitNe8EaS1" +
            "01HotAgMBAAECggEAeZOuVKSj3E9Wtjwqu3kZ/AYnUpXCAQfIRWkbsxfDFZR+ibb3WiVxV" +
            "YeFKLwSiy/VHcu0xWaNxY//4SE4JsJOXpyQ/OCfrJEfSZR1WEvRyYuAbGsVYZ8XHh7uI55B" +
            "pJMWmo1X4+wdqvawdk13CGh6o7n/ibMhtEPEEQM1Fxpv+NBoHuivLRVwu5fszWbTrzU+Kldp" +
            "xMiE9cVR6uQ4Ha207Y+lfdVqgOd+WXNydxqPIbfTCiAidC9+Ic0SI1F+/M+/u6TQx4AseC6o" +
            "BuLf/xNl4RVMccym/OWm6Dp3x1h9vPvVORJaZcKAhyQnDYZRjyy6uKPwuQJY1Wb25ZOIaQpm" +
            "4QKBgQDR1rtEpj0LlxOvE7td2ctBa9lDvQfH1QNcTHfX7rG3+wFMh2L3DYYVBkqKIpCgQxsbr" +
            "PEDRFaEDOqRbgtb3CvXr526ViZzmYGpQllfdL8HHq8imFIyclq5sC+PJ7YYHSlWkUvtckcPOm" +
            "S5wErK4a2bWb9zL/WJDwxnmUD0M3W6hQKBgQCvxPE8f/RhZX6y8VkazhNgvk01r4w9xgSWOf3" +
            "LcUDNkofdKVAN4Y6WkFdpdaK6ZDE6tYm5sE2hAJtpItWd3pQm/v6RX6u73E3q5R1mqfH4ObMyQ9" +
            "Cmp2/kdaCWH88tqLTrpzkEL6prjdAKxcEXkUWn4lCLmC3dfwv2PqDceQfViQKBgQDIFtXjFBFJp" +
            "/mhDFOxnifubgRyTyHILxpRPKlaQcU9Bh+q9b4sjby495LEPUljSBnlPfdjghptOFPD8QbrxPoZ" +
            "ZOAl+lgYCtxUpWvLVeuBwoSFnHFkoR5ZyeLPwrIkNEpivZGFDV8s6iV56h5nbuK4K9Ae6jBbTPo" +
            "rHzuMoDU2cQKBgQCuJN6phVB3itpLqikREKlMSV8v2HyG24DB4jTccpm00v+/E+ZXymi+LcYdG0q" +
            "dhxTrSN9qVQ2kfXh8YuHoJpT8Sdmn6HS6jJu6JUPK6ods2qX7IAtCglOuswfmlR9r6mFNtQEdb6h" +
            "WJUvA0IrtsaWknuoUVDLk57HPR9+ScGtdmQKBgDtDGfzMmpv0bzjf6jSQbTQLKiTq+qcbqQ1WMlu" +
            "LFTQpnYr8zmV3r2J7iSv1e6L1OZC1D8pe24K3tzQ1mYUqhIUMMpjiK6u1q8Jykj0ty6pUtuHwo0t" +
            "DSD8xyCNU6g86rEAhrUlN4qrYUZn6KVHLu5guFjcq9n0ss4BYqjg3pVqd";
    public static String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApabY3/oQLwaw3ugYnr" +
            "bP0XC4ox/NkT6M4/nKEvxMcZ6+bbnNoCdTVm2BzAoSX7oOfn8vmfGnmELgwVtRp+VHuLoNS0jLuUCDQ" +
            "qzwJP2RxF+3EYFrvCU9Ykukj2SvqRxr+OTkh1zLNT6EWMkjYTsBlaHgJA9LmnVz8UV3DG+4sLpy8zTf" +
            "+6iS8HD1bHqtVo4NU/WjOuEdNy7Dv0Va1h/8KxMnCkhfS5ZmKiYB40Lci/Qou/B074DPbsE3MDGb8Hj" +
            "J7mH05FjKUUjsmi/HEa6Nn/pfk7cSG/j41hyDH8wQR21UN2+8IhuxjnhGs6ohr1MqjLiZuYFo67oL5AWSyBVQLQIDAQAB";

    public static void main(String[] args) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","2021004132618212",privateKey,"json","UTF-8",alipayPublicKey,"RSA2");
        AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
        request.setNotifyUrl("");
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "20210817010101003");
        bizContent.put("total_amount", 0.01);
        bizContent.put("subject", "测试商品");
        bizContent.put("timeout_express", "10m");
        bizContent.put("product_code", "JSAPI_PAY");

//// 商品明细信息，按需传入
//JSONArray goodsDetail = new JSONArray();
//JSONObject goods1 = new JSONObject();
//goods1.put("goods_id", "goodsNo1");
//goods1.put("goods_name", "子商品1");
//goods1.put("quantity", 1);
//goods1.put("price", 0.01);
//goodsDetail.add(goods1);
//bizContent.put("goods_detail", goodsDetail);

//// 扩展信息，按需传入
//JSONObject extendParams = new JSONObject();
//extendParams.put("sys_service_provider_id", "2088511833207846");
//bizContent.put("extend_params", extendParams);

//// 结算信息，按需传入
//JSONObject settleInfo = new JSONObject();
//JSONArray settleDetailInfos = new JSONArray();
//JSONObject settleDetail = new JSONObject();
//settleDetail.put("trans_in_type", "defaultSettle");
//settleDetail.put("amount", 0.01);
//settleDetailInfos.add(settleDetail);
//settleInfo.put("settle_detail_infos", settleDetailInfos);
//bizContent.put("settle_info", settleInfo);

//// 二级商户信息，按需传入
//JSONObject subMerchant = new JSONObject();
//subMerchant.put("merchant_id", "2088000603999128");
//bizContent.put("sub_merchant", subMerchant);

//// 业务参数信息，按需传入
//JSONObject businessParams = new JSONObject();
//businessParams.put("busi_params_key", "busiParamsValue");
//bizContent.put("business_params", businessParams);

//// 营销信息，按需传入
//JSONObject promoParams = new JSONObject();
//promoParams.put("promo_params_key", "promoParamsValue");
//bizContent.put("promo_params", promoParams);

        request.setBizContent(bizContent.toString());
        AlipayTradeCreateResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }


//      //生成支付订单接口
//    public static String pay(Map<String,String> map) throws AlipayApiException {
//        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","2021004132618212",privateKey,"json","UTF-8",alipayPublicKey,"RSA2");
//        AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
//        request.setNotifyUrl("http://192.168.0.60:7000/api/alipayNotify");
//        JSONObject bizContent = new JSONObject();
//        bizContent.put("out_trade_no", map.get("paymentDetailsNo"));
//        bizContent.put("total_amount", map.get("totalAmount"));
//        bizContent.put("subject", map.get("subject"));
////        bizContent.put("buyer_id", map.get("buyer_id"));
//        bizContent.put("timeout_express", "10m");
//        bizContent.put("product_code", "JSAPI_PAY");
//
//        //// 商品明细信息，按需传入
//        //JSONArray goodsDetail = new JSONArray();
//        //JSONObject goods1 = new JSONObject();
//        //goods1.put("goods_id", "goodsNo1");
//        //goods1.put("goods_name", "子商品1");
//        //goods1.put("quantity", 1);
//        //goods1.put("price", 0.01);
//        //goodsDetail.add(goods1);
//        //bizContent.put("goods_detail", goodsDetail);
//
//        //// 扩展信息，按需传入
//        //JSONObject extendParams = new JSONObject();
//        //extendParams.put("sys_service_provider_id", "2088511833207846");
//        //bizContent.put("extend_params", extendParams);
//
//        //// 结算信息，按需传入
//        //JSONObject settleInfo = new JSONObject();
//        //JSONArray settleDetailInfos = new JSONArray();
//        //JSONObject settleDetail = new JSONObject();
//        //settleDetail.put("trans_in_type", "defaultSettle");
//        //settleDetail.put("amount", 0.01);
//        //settleDetailInfos.add(settleDetail);
//        //settleInfo.put("settle_detail_infos", settleDetailInfos);
//        //bizContent.put("settle_info", settleInfo);
//
//        //// 二级商户信息，按需传入
//        //JSONObject subMerchant = new JSONObject();
//        //subMerchant.put("merchant_id", "2088000603999128");
//        //bizContent.put("sub_merchant", subMerchant);
//
//        //// 业务参数信息，按需传入
//        //JSONObject businessParams = new JSONObject();
//        //businessParams.put("busi_params_key", "busiParamsValue");
//        //bizContent.put("business_params", businessParams);
//
//        //// 营销信息，按需传入
//        //JSONObject promoParams = new JSONObject();
//        //promoParams.put("promo_params_key", "promoParamsValue");
//        //bizContent.put("promo_params", promoParams);
//
//        request.setBizContent(bizContent.toString());
//        AlipayTradeCreateResponse response = alipayClient.execute(request);
//        if(response.isSuccess()){
//            System.out.println("调用成功");
//        } else {
//            System.out.println("调用失败");
//            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
//            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
//            // System.out.println(diagnosisUrl);
//        }
//        return response.getBody();
//    }

    //关闭订单
    public void closePayTest() throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2");
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("trade_no", "2013112611001004680073956707");
        request.setBizContent(bizContent.toString());
        AlipayTradeCloseResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }

//    //生成支付订单接口
//    public static String pay(Map<String,String> map) throws AlipayApiException {
//        // 初始化SDK
//        String privateKey  = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIB" +
//                "AQCQEzgoC7Rdpq7FrsiE7UXjz0yn32VPbcPpSXtg+wiqyo7op0ex9T+7h/jam49vz" +
//                "pmp/Ka0zE27P5YqsbJHO90G9jeLw0548Swo4e3sJZQ2403t+OOMbJnNCzCJvmAQ1aeCc" +
//                "QsVSfvucsUU3N37BrGeCwqfTDlEijAP9SQHfzJ8TCY5FWT2PTEg7A79fcJh91TFDf3nV" +
//                "SA6gxxi+7DRKxY3YydtyTnye6rr6/vDQWUk6pW1s4G6ftrJKs4KfJnKE1WD9zd8xvD5c" +
//                "WNObkigVF/sV9JFOaGXgDDrlNj8vCoE9cehHq5kQZT4tl9eRoCmHJ0H0Bor7nitNe8EaS1" +
//                "01HotAgMBAAECggEAeZOuVKSj3E9Wtjwqu3kZ/AYnUpXCAQfIRWkbsxfDFZR+ibb3WiVxV" +
//                "YeFKLwSiy/VHcu0xWaNxY//4SE4JsJOXpyQ/OCfrJEfSZR1WEvRyYuAbGsVYZ8XHh7uI55B" +
//                "pJMWmo1X4+wdqvawdk13CGh6o7n/ibMhtEPEEQM1Fxpv+NBoHuivLRVwu5fszWbTrzU+Kldp" +
//                "xMiE9cVR6uQ4Ha207Y+lfdVqgOd+WXNydxqPIbfTCiAidC9+Ic0SI1F+/M+/u6TQx4AseC6o" +
//                "BuLf/xNl4RVMccym/OWm6Dp3x1h9vPvVORJaZcKAhyQnDYZRjyy6uKPwuQJY1Wb25ZOIaQpm" +
//                "4QKBgQDR1rtEpj0LlxOvE7td2ctBa9lDvQfH1QNcTHfX7rG3+wFMh2L3DYYVBkqKIpCgQxsbr" +
//                "PEDRFaEDOqRbgtb3CvXr526ViZzmYGpQllfdL8HHq8imFIyclq5sC+PJ7YYHSlWkUvtckcPOm" +
//                "S5wErK4a2bWb9zL/WJDwxnmUD0M3W6hQKBgQCvxPE8f/RhZX6y8VkazhNgvk01r4w9xgSWOf3" +
//                "LcUDNkofdKVAN4Y6WkFdpdaK6ZDE6tYm5sE2hAJtpItWd3pQm/v6RX6u73E3q5R1mqfH4ObMyQ9" +
//                "Cmp2/kdaCWH88tqLTrpzkEL6prjdAKxcEXkUWn4lCLmC3dfwv2PqDceQfViQKBgQDIFtXjFBFJp" +
//                "/mhDFOxnifubgRyTyHILxpRPKlaQcU9Bh+q9b4sjby495LEPUljSBnlPfdjghptOFPD8QbrxPoZ" +
//                "ZOAl+lgYCtxUpWvLVeuBwoSFnHFkoR5ZyeLPwrIkNEpivZGFDV8s6iV56h5nbuK4K9Ae6jBbTPo" +
//                "rHzuMoDU2cQKBgQCuJN6phVB3itpLqikREKlMSV8v2HyG24DB4jTccpm00v+/E+ZXymi+LcYdG0q" +
//                "dhxTrSN9qVQ2kfXh8YuHoJpT8Sdmn6HS6jJu6JUPK6ods2qX7IAtCglOuswfmlR9r6mFNtQEdb6h" +
//                "WJUvA0IrtsaWknuoUVDLk57HPR9+ScGtdmQKBgDtDGfzMmpv0bzjf6jSQbTQLKiTq+qcbqQ1WMlu" +
//                "LFTQpnYr8zmV3r2J7iSv1e6L1OZC1D8pe24K3tzQ1mYUqhIUMMpjiK6u1q8Jykj0ty6pUtuHwo0t" +
//                "DSD8xyCNU6g86rEAhrUlN4qrYUZn6KVHLu5guFjcq9n0ss4BYqjg3pVqd";
//        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApabY3/oQLwaw3ugYnr" +
//                "bP0XC4ox/NkT6M4/nKEvxMcZ6+bbnNoCdTVm2BzAoSX7oOfn8vmfGnmELgwVtRp+VHuLoNS0jLuUCDQ" +
//                "qzwJP2RxF+3EYFrvCU9Ykukj2SvqRxr+OTkh1zLNT6EWMkjYTsBlaHgJA9LmnVz8UV3DG+4sLpy8zTf" +
//                "+6iS8HD1bHqtVo4NU/WjOuEdNy7Dv0Va1h/8KxMnCkhfS5ZmKiYB40Lci/Qou/B074DPbsE3MDGb8Hj" +
//                "J7mH05FjKUUjsmi/HEa6Nn/pfk7cSG/j41hyDH8wQR21UN2+8IhuxjnhGs6ohr1MqjLiZuYFo67oL5AWSyBVQLQIDAQAB";
//        AlipayConfig alipayConfig = new AlipayConfig();
//        alipayConfig.setServerUrl("http://192.168.0.60:7000/api/alipayNotify");
//        alipayConfig.setAppId("2021004132618212");
//        alipayConfig.setPrivateKey(privateKey);
//        alipayConfig.setFormat("json");
//        alipayConfig.setAlipayPublicKey(alipayPublicKey);
//        alipayConfig.setCharset("UTF-8");
//        alipayConfig.setSignType("RSA2");
//        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
//
//        // 构造请求参数以调用接口
//        AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
//        AlipayTradeCreateModel model = new AlipayTradeCreateModel();
//
//        // 设置商户订单号
//        model.setOutTradeNo(map.get("paymentDetailsNo"));
//
//        // 设置产品码
//        model.setProductCode("JSAPI_PAY");
//
//        // 设置小程序支付中
//        model.setOpAppId("2021004132618212");
//
//        // 设置订单总金额
//        model.setTotalAmount(map.get("totalAmount"));
//
//        //设置过期时间
//        model.setTimeoutExpress("15m");
//
////        // 设置业务扩展参数
////        ExtendParams extendParams = new ExtendParams();
////        extendParams.setTradeComponentOrderId("2023060801502300000008810000005657");
////        model.setExtendParams(extendParams);
////
////        // 设置可打折金额
////        model.setDiscountableAmount("80.00");
//
//        // 设置订单标题
//        model.setSubject(map.get("subject"));
//
////        // 设置订单附加信息
////        model.setBody("Iphone6 16G");
////
////        // 设置买家支付宝用户ID
////        model.setBuyerId("2088102146225135");
////
////        // 设置商户门店编号
////        model.setStoreId("NJ_001");
//
//
//        request.setBizModel(model);
//        AlipayTradeCreateResponse response = alipayClient.execute(request);
//        System.out.println(response.getBody());
//
//        if (response.isSuccess()) {
//            return response.getBody();
//        } else {
//            return "";
//            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
//            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
//            // System.out.println(diagnosisUrl);
//        }
//    }


    public static String pay(Map<String,String> map) throws AlipayApiException {
        // 初始化SDK
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("http://eiqp5b.natappfree.cc/api/alipayNotify");
        alipayConfig.setAppId("2021004132618212");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);

        // 构造请求参数以调用接口
        AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
        AlipayTradeCreateModel model = new AlipayTradeCreateModel();

        // 设置商户订单号
        model.setOutTradeNo(map.get("paymentDetailsNo"));

        // 设置产品码
        model.setProductCode("JSAPI_PAY");

        // 设置小程序支付中
        model.setOpAppId("2014072300007148");

        // 设置订单总金额
        model.setTotalAmount(map.get("totalAmount"));

//        // 设置业务扩展参数
//        ExtendParams extendParams = new ExtendParams();
//        extendParams.setTradeComponentOrderId("2023060801502300000008810000005657");
//        model.setExtendParams(extendParams);

//        // 设置可打折金额
//        model.setDiscountableAmount("80.00");

        // 设置订单标题
        model.setSubject(map.get("subject"));

//        // 设置订单附加信息
//        model.setBody("Iphone6 16G");

//        // 设置买家支付宝用户ID
//        model.setBuyerId("2088102146225135");

//        // 设置商户门店编号
//        model.setStoreId("NJ_001");

        request.setBizModel(model);
        AlipayTradeCreateResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
            return response.getBody();
        } else {
            System.out.println("调用失败");
            return "";
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }


}